import com.cap1.Person;
import com.cap1.config.MainConfig;
import com.cap10.aop.Calculator;
import com.cap10.config.Cap10MainConfig;
import com.cap12.config.Cap12MainConfig;
import com.cap2.config.Cap2MainConfig;
import com.cap3.config.Cap3MainConfig;
import com.cap4.config.Cap4MainConfig;
import com.cap5.config.Cap5MainConfig;
import com.cap6.config.Cap6MainConfig;
import com.cap7.config.Cap7MainConfigOfLifeCycle;
import com.cap8.bean.Bird;
import com.cap8.config.Cap8MainConfig;
import com.cap9.config.Cap9MainConfig;
import com.cap9.dao.TestDao;
import com.cap9.service.TestService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

public class MainTest1 {
    /**
     * 配置文件加载
     */
    @Test
    public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Person person1 = (Person) context.getBean("person1");
        System.out.println(person1);
    }

    /**
     * 配置类加载
     */
    @Test
    public void test2() {
        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        Person person = (Person) context.getBean("person");
        System.out.println(person);

        String[] name = context.getBeanNamesForType(Person.class);
        for (String s : name) {
            System.out.println(s);
        }
    }

    /**
     * 测试扫描器
     */
    @Test
    public void test3() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Cap2MainConfig.class);
        String[] name = app.getBeanDefinitionNames();
        for (String s : name) {
            System.out.println(s);
        }
    }

    /**
     * 多实例与单实例测试
     */
    @Test
    public void test4() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Cap3MainConfig.class);
        String[] name = app.getBeanDefinitionNames();
        for (String s : name) {
            System.out.println(s);
        }
        //从容器中分别取两次bean然后判断是否为同一个bean
        Object b1 = app.getBean("person");
        Object b2 = app.getBean("person");
        System.out.println(b1 == b2);
    }

    /**
     * 懒加载
     */
    @Test
    public void test5() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Cap4MainConfig.class);
        System.out.println("IOC容器创建完成........");
        app.getBean("person");//执行获取的时候才创建并初始化bean
    }

    /**
     * 按条件加载
     */
    @Test
    public void test6() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Cap5MainConfig.class);
        System.out.println("IOC容器创建完成........");
        app.close();
    }

    /**
     * Import组件
     */
    @Test
    public void test7() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Cap6MainConfig.class);
        System.out.println("IOC容器创建完成........");
        String[] beanDefinitionNames = app.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name);
        }
        Object bean = app.getBean("jamesFactoryBean");
        Object bean1 = app.getBean("jamesFactoryBean");
        System.out.println(bean == bean1);
        Class<?> type = app.getType("jamesFactoryBean");
        System.out.println("Bean的名称 " + bean + "\nbean的类型 " + type);
    }

    /**
     * Bean的生命周期
     */
    @Test
    public void test8() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Cap7MainConfigOfLifeCycle.class);
        System.out.println("IOC容器创建完成........");
        app.close();
    }

    /**
     * 测试@Value
     */
    @Test
    public void test9() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Cap8MainConfig.class);
        System.out.println("IOC容器创建完成........");
        String[] names = app.getBeanDefinitionNames();
        for (String s : names) {
            System.out.println(s);
        }

        Bird bird = app.getBean("bird", Bird.class);
        System.out.println(bird.toString());

        //获得系统环境变量
        ConfigurableEnvironment appEnvironment = app.getEnvironment();
        System.out.println(appEnvironment.getProperty("bird.color"));
    }

    /**
     * 测试@Autowired
     */
    @Test
    public void test10() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Cap9MainConfig.class);
        System.out.println("IOC容器创建完成........");
        TestService service = app.getBean("testService", TestService.class);
        service.println();
        //直接从容器中取TestDao,和使用@Autowired注解来取比较是否是同一个对象
        TestDao dao = app.getBean("testDao", TestDao.class);
        System.out.println(dao);
    }

    /**
     * 实现ApplicationContextAware与BeanNameAware接口
     */
    @Test
    public void test11() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Cap9MainConfig.class);
        System.out.println("IOC容器创建完成........");
    }

    /**
     * 测试AOP
     */
    @Test
    public void test12() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Cap10MainConfig.class);
        System.out.println("IOC容器创建完成........");
        Calculator calculator = app.getBean("calculator", Calculator.class);
        int result = calculator.div(4, 0);
        System.out.println(result);
        app.close();
    }

    @Test
    public void test13() {
        while (true) {
            System.out.println(11);
        }
    }

    @Test
    public void test14() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Cap12MainConfig.class);
        System.out.println("IOC容器创建完成........");
    }
}
